if (!require("pacman")) install.packages("pacman")
Loading required package: pacman
pacman::p_load(tidyverse, lubridate, glue, scales, dplyr, ggthemes, # (above) necessary libs RColorBrewer, # Divergence color pallete forcats, # reorder function ggimage, # change background with image janitor # clean data ) # set theme for ggplot2ggplot2::theme_set(ggplot2::theme_minimal(base_size =14))# set width of code outputoptions(width =65)# set figure parameters for knitrknitr::opts_chunk$set(fig.width =7, # 7" widthfig.asp =0.618, # the golden ratiofig.retina =3, # dpi multiplier for displaying HTML output on retinafig.align ="center", # center align figuresdpi =300# higher dpi, sharper image)
1 - Du Bois challenge.
income <-read.csv("data/income.csv")# Add "average_income" to Class Labelsincome <- income |>mutate(Other =replace_na(Other, 0),income_label =paste0("$", format(Average_Income, big.mark =",")),ClassLabel =paste0(Class, " | ", income_label),ClassLabel =factor(ClassLabel, levels =unique(ClassLabel)) )# Define Category order and colorsordered_categories <-c("Other", "Tax", "Clothes", "Food", "Rent")category_colors <-c(Rent ="#161616",Food ="#7d6683",Clothes ="#b78a77",Tax ="#a9a09d",Other ="#bdb09f")# Prepare income data for bar plotincome_clean <- income |>select(ClassLabel, Rent, Food, Clothes, Tax, Other) |>pivot_longer(cols = Rent:Other, names_to ="Category", values_to ="Percent") |>mutate(Category =factor(Category, levels = ordered_categories)) |>group_by(ClassLabel) |>mutate(pos =cumsum(Percent) - Percent /2) |>ungroup()# Plotincome_plot <- income_clean |>ggplot(aes(x =fct_rev(ClassLabel), y = Percent, fill = Category)) +geom_bar(stat ="identity", position ="stack", color ="black",width =0.7 ) +geom_text(data = income_clean |>filter(Percent >1),aes(label =paste0(round(Percent, 1), "%"), y = pos),color ="white", size =2 ) +scale_fill_manual(values = category_colors) +coord_flip(xlim =c(0, 7)) +scale_y_continuous(labels =NULL, expand =expansion(mult =c(0.1, 0.1))) +# Place Social Labelsannotate("text", x =1, y =102, label ="Well-To-Do", size =2, angle =90 ) +annotate("text", x =2.5, y =102, label ="Comfortable", size =2,angle =90 ) +annotate("text", x =4.5, y =102, label ="Fair", size =2, angle =90 ) +annotate("text", x =6.5, y =102, label ="Poor", size =2, angle =90 ) +# Bottom text annotationannotate("text", x =0, y =45, label ="FOR FURTHER STATISTICS RAISE THIS FRAME.", size =2 ) +labs(title ="INCOME AND EXPENDITURE OF 150 BLACK FAMILIES IN ATLANTA,G.A.,U.S.A.",subtitle ="Reproducing Du Bois",x ="",y ="",fill ="Annual Expenditure For" ) +theme_minimal(base_size =14) +theme(plot.title.position ="plot",plot.title =element_text(hjust =0, size =12, face ="bold"),axis.title.y =element_blank(),axis.text.y =element_text(face ="bold", size =7),panel.grid =element_blank(),legend.position ="top",legend.title.position ="top",legend.title =element_text(hjust =0.5), ) +guides(fill =guide_legend(reverse =TRUE))# Load background imageincome_plot <-ggbackground(income_plot, "images/parchment_paper.jpg")print(income_plot)